-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
decoder.js
1776 lines (1419 loc) · 52.9 KB
/
decoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.decode = decode;
var _helperApiError = require("@webassemblyjs/helper-api-error");
var ieee754 = _interopRequireWildcard(require("@webassemblyjs/ieee754"));
var utf8 = _interopRequireWildcard(require("@webassemblyjs/utf8"));
var t = _interopRequireWildcard(require("@webassemblyjs/ast"));
var _leb = require("@webassemblyjs/leb128");
var _helperWasmBytecode = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function toHex(n) {
return "0x" + Number(n).toString(16);
}
function byteArrayEq(l, r) {
if (l.length !== r.length) {
return false;
}
for (var i = 0; i < l.length; i++) {
if (l[i] !== r[i]) {
return false;
}
}
return true;
}
function decode(ab, opts) {
var buf = new Uint8Array(ab);
var getUniqueName = t.getUniqueNameGenerator();
var offset = 0;
function getPosition() {
return {
line: -1,
column: offset
};
}
function dump(b, msg) {
if (opts.dump === false) return;
var pad = "\t\t\t\t\t\t\t\t\t\t";
var str = "";
if (b.length < 5) {
str = b.map(toHex).join(" ");
} else {
str = "...";
}
console.log(toHex(offset) + ":\t", str, pad, ";", msg);
}
function dumpSep(msg) {
if (opts.dump === false) return;
console.log(";", msg);
}
/**
* TODO(sven): we can atually use a same structure
* we are adding incrementally new features
*/
var state = {
elementsInFuncSection: [],
elementsInExportSection: [],
elementsInCodeSection: [],
/**
* Decode memory from:
* - Memory section
*/
memoriesInModule: [],
/**
* Decoded types from:
* - Type section
*/
typesInModule: [],
/**
* Decoded functions from:
* - Function section
* - Import section
*/
functionsInModule: [],
/**
* Decoded tables from:
* - Table section
*/
tablesInModule: [],
/**
* Decoded globals from:
* - Global section
*/
globalsInModule: []
};
function isEOF() {
return offset >= buf.length;
}
function eatBytes(n) {
offset = offset + n;
}
function readBytesAtOffset(_offset, numberOfBytes) {
var arr = [];
for (var i = 0; i < numberOfBytes; i++) {
arr.push(buf[_offset + i]);
}
return arr;
}
function readBytes(numberOfBytes) {
return readBytesAtOffset(offset, numberOfBytes);
}
function readF64() {
var bytes = readBytes(ieee754.NUMBER_OF_BYTE_F64);
var value = ieee754.decodeF64(bytes);
if (Math.sign(value) * value === Infinity) {
return {
value: Math.sign(value),
inf: true,
nextIndex: ieee754.NUMBER_OF_BYTE_F64
};
}
if (isNaN(value)) {
var sign = bytes[bytes.length - 1] >> 7 ? -1 : 1;
var mantissa = 0;
for (var i = 0; i < bytes.length - 2; ++i) {
mantissa += bytes[i] * Math.pow(256, i);
}
mantissa += bytes[bytes.length - 2] % 16 * Math.pow(256, bytes.length - 2);
return {
value: sign * mantissa,
nan: true,
nextIndex: ieee754.NUMBER_OF_BYTE_F64
};
}
return {
value: value,
nextIndex: ieee754.NUMBER_OF_BYTE_F64
};
}
function readF32() {
var bytes = readBytes(ieee754.NUMBER_OF_BYTE_F32);
var value = ieee754.decodeF32(bytes);
if (Math.sign(value) * value === Infinity) {
return {
value: Math.sign(value),
inf: true,
nextIndex: ieee754.NUMBER_OF_BYTE_F32
};
}
if (isNaN(value)) {
var sign = bytes[bytes.length - 1] >> 7 ? -1 : 1;
var mantissa = 0;
for (var i = 0; i < bytes.length - 2; ++i) {
mantissa += bytes[i] * Math.pow(256, i);
}
mantissa += bytes[bytes.length - 2] % 128 * Math.pow(256, bytes.length - 2);
return {
value: sign * mantissa,
nan: true,
nextIndex: ieee754.NUMBER_OF_BYTE_F32
};
}
return {
value: value,
nextIndex: ieee754.NUMBER_OF_BYTE_F32
};
}
function readUTF8String() {
var lenu32 = readU32(); // Don't eat any bytes. Instead, peek ahead of the current offset using
// readBytesAtOffset below. This keeps readUTF8String neutral with respect
// to the current offset, just like the other readX functions.
var strlen = lenu32.value;
dump([strlen], "string length");
var bytes = readBytesAtOffset(offset + lenu32.nextIndex, strlen);
var value = utf8.decode(bytes);
return {
value: value,
nextIndex: strlen + lenu32.nextIndex
};
}
/**
* Decode an unsigned 32bits integer
*
* The length will be handled by the leb librairy, we pass the max number of
* byte.
*/
function readU32() {
var bytes = readBytes(_leb.MAX_NUMBER_OF_BYTE_U32);
var buffer = Buffer.from(bytes);
return (0, _leb.decodeUInt32)(buffer);
}
function readVaruint32() {
// where 32 bits = max 4 bytes
var bytes = readBytes(4);
var buffer = Buffer.from(bytes);
return (0, _leb.decodeUInt32)(buffer);
}
function readVaruint7() {
// where 7 bits = max 1 bytes
var bytes = readBytes(1);
var buffer = Buffer.from(bytes);
return (0, _leb.decodeUInt32)(buffer);
}
/**
* Decode a signed 32bits interger
*/
function read32() {
var bytes = readBytes(_leb.MAX_NUMBER_OF_BYTE_U32);
var buffer = Buffer.from(bytes);
return (0, _leb.decodeInt32)(buffer);
}
/**
* Decode a signed 64bits integer
*/
function read64() {
var bytes = readBytes(_leb.MAX_NUMBER_OF_BYTE_U64);
var buffer = Buffer.from(bytes);
return (0, _leb.decodeInt64)(buffer);
}
function readU64() {
var bytes = readBytes(_leb.MAX_NUMBER_OF_BYTE_U64);
var buffer = Buffer.from(bytes);
return (0, _leb.decodeUInt64)(buffer);
}
function readByte() {
return readBytes(1)[0];
}
function parseModuleHeader() {
if (isEOF() === true || offset + 4 > buf.length) {
throw new Error("unexpected end");
}
var header = readBytes(4);
if (byteArrayEq(_helperWasmBytecode.default.magicModuleHeader, header) === false) {
throw new _helperApiError.CompileError("magic header not detected");
}
dump(header, "wasm magic header");
eatBytes(4);
}
function parseVersion() {
if (isEOF() === true || offset + 4 > buf.length) {
throw new Error("unexpected end");
}
var version = readBytes(4);
if (byteArrayEq(_helperWasmBytecode.default.moduleVersion, version) === false) {
throw new _helperApiError.CompileError("unknown binary version");
}
dump(version, "wasm version");
eatBytes(4);
}
function parseVec(cast) {
var u32 = readU32();
var length = u32.value;
eatBytes(u32.nextIndex);
dump([length], "number");
if (length === 0) {
return [];
}
var elements = [];
for (var i = 0; i < length; i++) {
var byte = readByte();
eatBytes(1);
var value = cast(byte);
dump([byte], value);
if (typeof value === "undefined") {
throw new _helperApiError.CompileError("Internal failure: parseVec could not cast the value");
}
elements.push(value);
}
return elements;
} // Type section
// https://webassembly.github.io/spec/binary/modules.html#binary-typesec
function parseTypeSection(numberOfTypes) {
var typeInstructionNodes = [];
dump([numberOfTypes], "num types");
for (var i = 0; i < numberOfTypes; i++) {
var _startLoc = getPosition();
dumpSep("type " + i);
var type = readByte();
eatBytes(1);
if (type == _helperWasmBytecode.default.types.func) {
dump([type], "func");
var paramValtypes = parseVec(function (b) {
return _helperWasmBytecode.default.valtypes[b];
});
var params = paramValtypes.map(function (v) {
return t.funcParam(
/*valtype*/
v);
});
var result = parseVec(function (b) {
return _helperWasmBytecode.default.valtypes[b];
});
typeInstructionNodes.push(function () {
var endLoc = getPosition();
return t.withLoc(t.typeInstruction(undefined, t.signature(params, result)), endLoc, _startLoc);
}());
state.typesInModule.push({
params: params,
result: result
});
} else {
throw new Error("Unsupported type: " + toHex(type));
}
}
return typeInstructionNodes;
} // Import section
// https://webassembly.github.io/spec/binary/modules.html#binary-importsec
function parseImportSection(numberOfImports) {
var imports = [];
for (var i = 0; i < numberOfImports; i++) {
dumpSep("import header " + i);
var _startLoc2 = getPosition();
/**
* Module name
*/
var moduleName = readUTF8String();
eatBytes(moduleName.nextIndex);
dump([], "module name (".concat(moduleName.value, ")"));
/**
* Name
*/
var name = readUTF8String();
eatBytes(name.nextIndex);
dump([], "name (".concat(name.value, ")"));
/**
* Import descr
*/
var descrTypeByte = readByte();
eatBytes(1);
var descrType = _helperWasmBytecode.default.importTypes[descrTypeByte];
dump([descrTypeByte], "import kind");
if (typeof descrType === "undefined") {
throw new _helperApiError.CompileError("Unknown import description type: " + toHex(descrTypeByte));
}
var importDescr = void 0;
if (descrType === "func") {
var indexU32 = readU32();
var typeindex = indexU32.value;
eatBytes(indexU32.nextIndex);
dump([typeindex], "type index");
var signature = state.typesInModule[typeindex];
if (typeof signature === "undefined") {
throw new _helperApiError.CompileError("function signature not found (".concat(typeindex, ")"));
}
var id = getUniqueName("func");
importDescr = t.funcImportDescr(id, t.signature(signature.params, signature.result));
state.functionsInModule.push({
id: t.identifier(name.value),
signature: signature,
isExternal: true
});
} else if (descrType === "global") {
importDescr = parseGlobalType();
var globalNode = t.global(importDescr, []);
state.globalsInModule.push(globalNode);
} else if (descrType === "table") {
importDescr = parseTableType(i);
} else if (descrType === "mem") {
var memoryNode = parseMemoryType(0);
state.memoriesInModule.push(memoryNode);
importDescr = memoryNode;
} else {
throw new _helperApiError.CompileError("Unsupported import of type: " + descrType);
}
imports.push(function () {
var endLoc = getPosition();
return t.withLoc(t.moduleImport(moduleName.value, name.value, importDescr), endLoc, _startLoc2);
}());
}
return imports;
} // Function section
// https://webassembly.github.io/spec/binary/modules.html#function-section
function parseFuncSection(numberOfFunctions) {
dump([numberOfFunctions], "num funcs");
for (var i = 0; i < numberOfFunctions; i++) {
var indexU32 = readU32();
var typeindex = indexU32.value;
eatBytes(indexU32.nextIndex);
dump([typeindex], "type index");
var signature = state.typesInModule[typeindex];
if (typeof signature === "undefined") {
throw new _helperApiError.CompileError("function signature not found (".concat(typeindex, ")"));
} // preserve anonymous, a name might be resolved later
var id = t.withRaw(t.identifier(getUniqueName("func")), "");
state.functionsInModule.push({
id: id,
signature: signature,
isExternal: false
});
}
} // Export section
// https://webassembly.github.io/spec/binary/modules.html#export-section
function parseExportSection(numberOfExport) {
dump([numberOfExport], "num exports"); // Parse vector of exports
for (var i = 0; i < numberOfExport; i++) {
var _startLoc3 = getPosition();
/**
* Name
*/
var name = readUTF8String();
eatBytes(name.nextIndex);
dump([], "export name (".concat(name.value, ")"));
/**
* exportdescr
*/
var typeIndex = readByte();
eatBytes(1);
dump([typeIndex], "export kind");
var indexu32 = readU32();
var index = indexu32.value;
eatBytes(indexu32.nextIndex);
dump([index], "export index");
var id = void 0,
signature = void 0;
if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Func") {
var func = state.functionsInModule[index];
if (typeof func === "undefined") {
throw new _helperApiError.CompileError("unknown function (".concat(index, ")"));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = func.signature;
} else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Table") {
var table = state.tablesInModule[index];
if (typeof table === "undefined") {
throw new _helperApiError.CompileError("unknown table ".concat(index));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Mem") {
var memNode = state.memoriesInModule[index];
if (typeof memNode === "undefined") {
throw new _helperApiError.CompileError("unknown memory ".concat(index));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Global") {
var global = state.globalsInModule[index];
if (typeof global === "undefined") {
throw new _helperApiError.CompileError("unknown global ".concat(index));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else {
console.warn("Unsupported export type: " + toHex(typeIndex));
return;
}
var endLoc = getPosition();
state.elementsInExportSection.push({
name: name.value,
type: _helperWasmBytecode.default.exportTypes[typeIndex],
signature: signature,
id: id,
index: index,
endLoc: endLoc,
startLoc: _startLoc3
});
}
} // Code section
// https://webassembly.github.io/spec/binary/modules.html#code-section
function parseCodeSection(numberOfFuncs) {
dump([numberOfFuncs], "number functions"); // Parse vector of function
for (var i = 0; i < numberOfFuncs; i++) {
var _startLoc4 = getPosition();
dumpSep("function body " + i); // the u32 size of the function code in bytes
// Ignore it for now
var bodySizeU32 = readU32();
eatBytes(bodySizeU32.nextIndex);
dump([bodySizeU32.value], "function body size");
var code = [];
/**
* Parse locals
*/
var funcLocalNumU32 = readU32();
var funcLocalNum = funcLocalNumU32.value;
eatBytes(funcLocalNumU32.nextIndex);
dump([funcLocalNum], "num locals");
var locals = [];
for (var _i = 0; _i < funcLocalNum; _i++) {
var _startLoc5 = getPosition();
var localCountU32 = readU32();
var localCount = localCountU32.value;
eatBytes(localCountU32.nextIndex);
dump([localCount], "num local");
var valtypeByte = readByte();
eatBytes(1);
var type = _helperWasmBytecode.default.valtypes[valtypeByte];
var args = [];
for (var _i2 = 0; _i2 < localCount; _i2++) {
args.push(t.valtypeLiteral(type));
}
var localNode = function () {
var endLoc = getPosition();
return t.withLoc(t.instruction("local", args), endLoc, _startLoc5);
}();
locals.push(localNode);
dump([valtypeByte], type);
if (typeof type === "undefined") {
throw new _helperApiError.CompileError("Unexpected valtype: " + toHex(valtypeByte));
}
}
code.push.apply(code, locals); // Decode instructions until the end
parseInstructionBlock(code);
var endLoc = getPosition();
state.elementsInCodeSection.push({
code: code,
locals: locals,
endLoc: endLoc,
startLoc: _startLoc4,
bodySize: bodySizeU32.value
});
}
}
function parseInstructionBlock(code) {
while (true) {
var _startLoc6 = getPosition();
var instructionAlreadyCreated = false;
var instructionByte = readByte();
eatBytes(1);
if (instructionByte === 0xfe) {
throw new _helperApiError.CompileError("Atomic instructions are not implemented");
}
var instruction = _helperWasmBytecode.default.symbolsByByte[instructionByte];
if (typeof instruction === "undefined") {
throw new _helperApiError.CompileError("Unexpected instruction: " + toHex(instructionByte));
}
if (typeof instruction.object === "string") {
dump([instructionByte], "".concat(instruction.object, ".").concat(instruction.name));
} else {
dump([instructionByte], instruction.name);
}
/**
* End of the function
*/
if (instruction.name === "end") {
var node = function () {
var endLoc = getPosition();
return t.withLoc(t.instruction(instruction.name), endLoc, _startLoc6);
}();
code.push(node);
break;
}
var args = [];
if (instruction.name === "loop") {
var _startLoc7 = getPosition();
var blocktypeByte = readByte();
eatBytes(1);
var blocktype = _helperWasmBytecode.default.blockTypes[blocktypeByte];
dump([blocktypeByte], "blocktype");
if (typeof blocktype === "undefined") {
throw new _helperApiError.CompileError("Unexpected blocktype: " + toHex(blocktypeByte));
}
var instr = [];
parseInstructionBlock(instr); // preserve anonymous
var label = t.withRaw(t.identifier(getUniqueName("loop")), "");
var loopNode = function () {
var endLoc = getPosition();
return t.withLoc(t.loopInstruction(label, blocktype, instr), endLoc, _startLoc7);
}();
code.push(loopNode);
instructionAlreadyCreated = true;
} else if (instruction.name === "if") {
var _startLoc8 = getPosition();
var _blocktypeByte = readByte();
eatBytes(1);
var _blocktype = _helperWasmBytecode.default.blockTypes[_blocktypeByte];
dump([_blocktypeByte], "blocktype");
if (typeof _blocktype === "undefined") {
throw new _helperApiError.CompileError("Unexpected blocktype: " + toHex(_blocktypeByte));
}
var testIndex = t.withRaw(t.identifier(getUniqueName("if")), "");
var ifBody = [];
parseInstructionBlock(ifBody); // Defaults to no alternate
var elseIndex = 0;
for (elseIndex = 0; elseIndex < ifBody.length; ++elseIndex) {
var _instr = ifBody[elseIndex];
if (_instr.type === "Instr" && _instr.id === "else") {
break;
}
}
var consequentInstr = ifBody.slice(0, elseIndex);
var alternate = ifBody.slice(elseIndex + 1); // wast sugar
var testInstrs = [];
var ifNode = function () {
var endLoc = getPosition();
return t.withLoc(t.ifInstruction(testIndex, testInstrs, _blocktype, consequentInstr, alternate), endLoc, _startLoc8);
}();
code.push(ifNode);
instructionAlreadyCreated = true;
} else if (instruction.name === "block") {
var _startLoc9 = getPosition();
var _blocktypeByte2 = readByte();
eatBytes(1);
var _blocktype2 = _helperWasmBytecode.default.blockTypes[_blocktypeByte2];
dump([_blocktypeByte2], "blocktype");
if (typeof _blocktype2 === "undefined") {
throw new _helperApiError.CompileError("Unexpected blocktype: " + toHex(_blocktypeByte2));
}
var _instr2 = [];
parseInstructionBlock(_instr2); // preserve anonymous
var _label = t.withRaw(t.identifier(getUniqueName("block")), "");
var blockNode = function () {
var endLoc = getPosition();
return t.withLoc(t.blockInstruction(_label, _instr2, _blocktype2), endLoc, _startLoc9);
}();
code.push(blockNode);
instructionAlreadyCreated = true;
} else if (instruction.name === "call") {
var indexu32 = readU32();
var index = indexu32.value;
eatBytes(indexu32.nextIndex);
dump([index], "index");
var callNode = function () {
var endLoc = getPosition();
return t.withLoc(t.callInstruction(t.indexLiteral(index)), endLoc, _startLoc6);
}();
code.push(callNode);
instructionAlreadyCreated = true;
} else if (instruction.name === "call_indirect") {
var _startLoc10 = getPosition();
var indexU32 = readU32();
var typeindex = indexU32.value;
eatBytes(indexU32.nextIndex);
dump([typeindex], "type index");
var signature = state.typesInModule[typeindex];
if (typeof signature === "undefined") {
throw new _helperApiError.CompileError("call_indirect signature not found (".concat(typeindex, ")"));
}
var _callNode = t.callIndirectInstruction(t.signature(signature.params, signature.result), []);
var flagU32 = readU32();
var flag = flagU32.value; // 0x00 - reserved byte
eatBytes(flagU32.nextIndex);
if (flag !== 0) {
throw new _helperApiError.CompileError("zero flag expected");
}
code.push(function () {
var endLoc = getPosition();
return t.withLoc(_callNode, endLoc, _startLoc10);
}());
instructionAlreadyCreated = true;
} else if (instruction.name === "br_table") {
var indicesu32 = readU32();
var indices = indicesu32.value;
eatBytes(indicesu32.nextIndex);
dump([indices], "num indices");
for (var i = 0; i <= indices; i++) {
var _indexu = readU32();
var _index = _indexu.value;
eatBytes(_indexu.nextIndex);
dump([_index], "index");
args.push(t.numberLiteralFromRaw(_indexu.value.toString(), "u32"));
}
} else if (instructionByte >= 0x28 && instructionByte <= 0x40) {
/**
* Memory instructions
*/
if (instruction.name === "grow_memory" || instruction.name === "current_memory") {
var _indexU = readU32();
var _index2 = _indexU.value;
eatBytes(_indexU.nextIndex);
if (_index2 !== 0) {
throw new Error("zero flag expected");
}
dump([_index2], "index");
} else {
var aligun32 = readU32();
var align = aligun32.value;
eatBytes(aligun32.nextIndex);
dump([align], "align");
var offsetu32 = readU32();
var _offset2 = offsetu32.value;
eatBytes(offsetu32.nextIndex);
dump([_offset2], "offset");
}
} else if (instructionByte >= 0x41 && instructionByte <= 0x44) {
/**
* Numeric instructions
*/
if (instruction.object === "i32") {
var value32 = read32();
var value = value32.value;
eatBytes(value32.nextIndex);
dump([value], "i32 value");
args.push(t.numberLiteralFromRaw(value));
}
if (instruction.object === "u32") {
var valueu32 = readU32();
var _value = valueu32.value;
eatBytes(valueu32.nextIndex);
dump([_value], "u32 value");
args.push(t.numberLiteralFromRaw(_value));
}
if (instruction.object === "i64") {
var value64 = read64();
var _value2 = value64.value;
eatBytes(value64.nextIndex);
dump([Number(_value2.toString())], "i64 value");
var high = _value2.high,
low = _value2.low;
var _node = {
type: "LongNumberLiteral",
value: {
high: high,
low: low
}
};
args.push(_node);
}
if (instruction.object === "u64") {
var valueu64 = readU64();
var _value3 = valueu64.value;
eatBytes(valueu64.nextIndex);
dump([Number(_value3.toString())], "u64 value");
var _high = _value3.high,
_low = _value3.low;
var _node2 = {
type: "LongNumberLiteral",
value: {
high: _high,
low: _low
}
};
args.push(_node2);
}
if (instruction.object === "f32") {
var valuef32 = readF32();
var _value4 = valuef32.value;
eatBytes(valuef32.nextIndex);
dump([_value4], "f32 value");
args.push( // $FlowIgnore
t.floatLiteral(_value4, valuef32.nan, valuef32.inf, String(_value4)));
}
if (instruction.object === "f64") {
var valuef64 = readF64();
var _value5 = valuef64.value;
eatBytes(valuef64.nextIndex);
dump([_value5], "f64 value");
args.push( // $FlowIgnore
t.floatLiteral(_value5, valuef64.nan, valuef64.inf, String(_value5)));
}
} else {
for (var _i3 = 0; _i3 < instruction.numberOfArgs; _i3++) {
var u32 = readU32();
eatBytes(u32.nextIndex);
dump([u32.value], "argument " + _i3);
args.push(t.numberLiteralFromRaw(u32.value));
}
}
if (instructionAlreadyCreated === false) {
if (typeof instruction.object === "string") {
var _node3 = function () {
var endLoc = getPosition();
return t.withLoc(t.objectInstruction(instruction.name, instruction.object, args), endLoc, _startLoc6);
}();
code.push(_node3);
} else {
var _node4 = function () {
var endLoc = getPosition();
return t.withLoc(t.instruction(instruction.name, args), endLoc, _startLoc6);
}();
code.push(_node4);
}
}
}
} // https://webassembly.github.io/spec/core/binary/types.html#limits
function parseLimits() {
var limitType = readByte();
eatBytes(1);
dump([limitType], "limit type");
var min, max;
if (limitType === 0x01 || limitType === 0x03 // shared limits
) {
var u32min = readU32();
min = parseInt(u32min.value);
eatBytes(u32min.nextIndex);
dump([min], "min");
var u32max = readU32();
max = parseInt(u32max.value);
eatBytes(u32max.nextIndex);
dump([max], "max");
}
if (limitType === 0x00) {
var _u32min = readU32();
min = parseInt(_u32min.value);
eatBytes(_u32min.nextIndex);
dump([min], "min");
}
return t.limit(min, max);
} // https://webassembly.github.io/spec/core/binary/types.html#binary-tabletype
function parseTableType(index) {
var name = t.withRaw(t.identifier(getUniqueName("table")), String(index));
var elementTypeByte = readByte();
eatBytes(1);
dump([elementTypeByte], "element type");
var elementType = _helperWasmBytecode.default.tableTypes[elementTypeByte];
if (typeof elementType === "undefined") {
throw new _helperApiError.CompileError("Unknown element type in table: " + toHex(elementType));
}
var limits = parseLimits();
return t.table(elementType, limits, name);
} // https://webassembly.github.io/spec/binary/types.html#global-types
function parseGlobalType() {
var valtypeByte = readByte();
eatBytes(1);
var type = _helperWasmBytecode.default.valtypes[valtypeByte];
dump([valtypeByte], type);
if (typeof type === "undefined") {
throw new _helperApiError.CompileError("Unknown valtype: " + toHex(valtypeByte));
}